package org.xukai.remoting.ws;



import javax.xml.namespace.QName;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ws4j {

    public static void main(String[] args) {
        System.setProperty("sun.net.client.defaultConnectTimeout", "888331");
        System.setProperty("sun.net.client.defaultReadTimeout", "8883331");
        String method = "demo";// 具体要调用某个流程
        String param = "<startDate>15</startDate>";
        String endpoint = "http://127.0.0.1:8076/webServices";
        String result = null;
        try {
            result = invoke( endpoint,method, param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }


    /**
     * 调用服务
     *
     * @param endpoint
     * @param method
     * @param param
     * @return
     * @throws Exception
     */
    private static String invoke(String endpoint, String method, String param)
            throws Exception {
        String ns = "http://www.guahao.com";
        String serviceName = "himapEntryService";
        String portName = "himapEntryPort";
        Source resp = null;
        long start = 0;
        try {
            QName port = new QName(ns, portName);
            Service service = Service.create(new QName(ns, serviceName));
            service.addPort(port, SOAPBinding.SOAP11HTTP_BINDING, endpoint);
            Dispatch<Source> disp = service.createDispatch(port, Source.class,Service.Mode.PAYLOAD);
            String request = String.format("<ns2:%1$s_Request xmlns:ns2=\"%2$s\"><request>%3$s</request></ns2:%1$s_Request>",
                            method, ns, param);
//            response = disp.invoke(new StreamSource(new StringReader(request)));
            StreamSource source = new StreamSource(new StringReader(request));
            Future<?> future = disp.invokeAsync(new StreamSource(new StringReader(request)));
            start = System.currentTimeMillis();
            resp = (Source)future.get();
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end-start) + "ms");
        } catch (Exception e) {
            e.printStackTrace();
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end-start) + "ms");
        }
        return sourceToXMLString(resp);
    }

    /**
     * 将source转换为字符串
     *
     * @param result
     * @return
     */
    private static String sourceToXMLString(Source result) throws Exception {
        String xmlResult = null;
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(out);

        transformer.transform(result, streamResult);
        xmlResult = out.toString("UTF-8");
        return xmlResult;
    }
}
